package com.example.address_list.slice;

import com.example.address_list.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.data.rdb.ValuesBucket;
import ohos.utils.net.Uri;

public class main_addSlice extends AbilitySlice {
    TextField id,name,age;
    Button button_add;
    DataAbilityHelper databaseHelper;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_add);
        databaseHelper= DataAbilityHelper.creator(this);
        id= (TextField) findComponentById(ResourceTable.Id_id);
        name= (TextField) findComponentById(ResourceTable.Id_age);
        age= (TextField) findComponentById(ResourceTable.Id_age);
        button_add.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String id_String = id.getText().toString();
                String name_String=name.getText().toString();
                String age_String=age.getText().toString();
                ValuesBucket valuesBucket=new ValuesBucket();
                valuesBucket.putString("userid",id_String);
                valuesBucket.putString("username",name_String);
                valuesBucket.putString("userage",age_String);
                try {
                    databaseHelper.insert(Uri.parse("dataability:///com.example.address_list.DataAbility/student"),valuesBucket);
                    Intent intent=new Intent();
                    present(new MainAbilitySlice(),intent);
                } catch (DataAbilityRemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
